﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Public Class MetaDataTable

    Private _metaData As XmlMetaData
    Private _node As XElement
    Public Sub New(ByVal node As XElement, ByVal metaData As XmlMetaData)
        _node = node
        _metaData = metaData
    End Sub

    Public ReadOnly Property MetaData() As XmlMetaData
        Get
            Return _metaData
        End Get
    End Property

    ''' <summary>
    ''' Gets the name that this table's type will use in .Net code
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MemberName() As String
        Get
            Return _node.Attribute("Member").Value
        End Get
    End Property

    ''' <summary>
    ''' Renames this table.
    ''' </summary>
    ''' <param name="newName">The new name for this table.</param>
    ''' <remarks></remarks>
    Public Sub RenameTo(ByVal newName As String)

        Dim node As XElement = _node
        Dim nameNode As String = "Member"

        If node.Attribute(nameNode) IsNot Nothing Then
            node.Attribute(nameNode).Value = newName
            Return
        End If

        Dim member As XAttribute = New XAttribute(nameNode, newName)
        node.Add(member)

    End Sub

    ''' <summary>
    ''' Finds a Type in this table with the given name
    ''' </summary>
    ''' <param name="name">The name of the type to find</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Type(ByVal name As String) As MetaDataType

        Dim q = From c In _node.Descendants(Names.TypeXName) _
                Where c.Attribute("Name") IsNot Nothing AndAlso c.Attribute("Name").Value.Equals(name, Strings.DefaultComparisonType) _
                Select c

        If q.Count = 0 Then
            Throw New TypeNotFoundException(name)
        End If

        Dim metaType As New MetaDataType(q.First(), _metaData)
        Return metaType

    End Function

    Public Function Types() As List(Of MetaDataType)

        Dim q = From c In _node.Descendants(Names.TypeXName) Select c

        Dim list As New List(Of MetaDataType)

        For Each el As XElement In q.ToList
            Dim metaType As New MetaDataType(el, _metaData)
            list.Add(metaType)
        Next

        Return list

    End Function

End Class
